package dbbaas;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {
	public static Connection getDBConnection(String name) {// ��ȡ��ݿ�����
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://localhost:3306/"
				+ name
				+ "?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=false";
		String userName = "root";
		String userPSW = "1";
		Connection conn = null;
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, userName, userPSW);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}

	public boolean add(AppEntity table) {// ��appconfig������һ�����
		Connection conn = null;
		PreparedStatement pstm = null;
		String sql = "insert into appconfig (appid,appname,appkey,note) values(?,?,?,?)";
		try {
			conn = getDBConnection("cloudservice");
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, table.getAppid());
			pstm.setString(2, table.getAppname());
			pstm.setString(3, table.getAppkey());
			pstm.setString(4, table.getNote());
			int flag = pstm.executeUpdate();
			if (flag > 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (pstm != null) {
					pstm.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return false;

	}

	public boolean addData(String dataName) {// 添加数据库
		Connection conn = null;
		PreparedStatement pstm = null;
		String sql = "create database " + dataName;
//				+ "?useUnicode=true&characterEncoding=UTF-8";
		try {
			conn = getDBConnection("cloudservice");
			pstm = conn.prepareStatement(sql);
			int flag = pstm.executeUpdate();
			if (flag > 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (pstm != null) {
					pstm.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	public boolean isExist(String appid) {// ��ѯ����Ƿ����
		Connection conn = null;
		String sql = "select * from appconfig where appid=\'" + appid + "\'";
		String sql1 = "SELECT * FROM information_schema.SCHEMATA where SCHEMA_NAME=\'"
				+ appid + "\'";
		try {
			conn = getDBConnection("cloudservice");
			PreparedStatement pstm = conn.prepareStatement(sql);
			// System.out.println(sql);
			PreparedStatement pstm1 = conn.prepareStatement(sql1);
			ResultSet rs = pstm.executeQuery();
			// System.out.println(rs);
			ResultSet rs1 = pstm1.executeQuery();
			// System.out.println(rs1);
			if (rs.next()) {
				// System.out.println("rs����");
				return true;
			} else if (rs1.next()) {
				// System.out.println("rs1����");
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// System.out.println("������");
		return false;
	}

	// public ArrayList<AppEntity> sel(String appid) {
	// Connection conn = null;
	// String sql = "select * from appconfig where appid=" + appid;
	// ArrayList<AppEntity> list = new ArrayList<AppEntity>();
	// try {
	// conn = getDBConnection("cloudservice");
	// PreparedStatement pstm = conn.prepareStatement(sql);
	// ResultSet rs = pstm.executeQuery();
	// if (rs.next()) {
	// AppEntity appentity = new AppEntity();
	// appentity.setAppid(rs.getString("appid"));
	// appentity.setAppname(rs.getString("appname"));
	// appentity.setAppkey(rs.getString("appkey"));
	// appentity.setNote(rs.getString("note"));
	// list.add(appentity);
	// }
	// } catch (SQLException e) {
	// e.printStackTrace();
	// }
	// return list;
	// }

}
